package com.itheima.dao;

import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class StudentTest {

    @Test
    public void deleteC() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();

        int delete = sqlSession.delete("myBatisStudent.deleteC", 1);

        sqlSession.commit();
        System.out.println(delete);

        resourceAsStream.close();
        sqlSession.close();


    }


    @Test
    public void updateB() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        Student student=new Student(1,"张里",23);
        int update = sqlSession.update("myBatisStudent.updateB", student);

        sqlSession.commit();

        System.out.println(update);

        sqlSession.close();
        resourceAsStream.close();


    }


    @Test
    public void insertA() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        Student student =new Student(7,"王七",29);
        int insert = sqlSession.insert("myBatisStudent.insert",student);

        sqlSession.commit();

        System.out.println(insert);

        resourceAsStream.close();
        sqlSession.close();


    }

    @Test
    public void selectBid() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        Object myBatisStudent = sqlSession.selectOne("myBatisStudent.selectBid", 2);

        System.out.println(myBatisStudent);
        resourceAsStream.close();
        sqlSession.close();


    }

    @Test
    public void selectAll() throws Exception{
        InputStream resourceAsStream = Resources.getResourceAsStream("myBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        List<Object> objects = sqlSession.selectList("myBatisStudent.selectAll");

        System.out.println(objects);

        resourceAsStream.close();
        sqlSession.close();

    }
}
